<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aCamForm = array();
require("_inc/const.campiAsez.php");
$aCamForm[10][0] = array('cod_sez', 'Codice', 4,10,'text',8, 8);
$aCamForm[30][0] = array('tipo_sez', 'Tipo', 4,16,'select',$aTipiSez);

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 

reqDef('cod_sez','');
reqDef('descr','');
reqDef('tipo_sez','int');

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('Gestione sezioni anagrafica','Nuova sezione',$msgPag,'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('id');?>
<?=getHtmlHidden('old_cod_sez');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Sezione anagrafica</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_sez'] = strtolower(trim(reqVar('cod_sez')));
	if (reqVar('cod_sez')=='') {
		$msg .= "Codice obbligatorio<br>";
	}
	if (preg_match("/[^-_a-z0-9]/",reqVar('cod_sez'))) {
		$msg = "Codice '".reqVar('cod_sez')."' puo' contenere solo lettere numeri '-' e '_'<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	if (preg_match("/[^0-9]/",reqVar('seq')) || reqVarTrim('seq')=='') {
		$msg = "campo 'Sequenza' non numerico o vuoto<br>";
	}
	$sqlSel = "SELECT cod_sez FROM ovf_anag_sez " .
			" WHERE cod_sez = '".reqVarAS('cod_sez')."'";
	$objDB->exec($sqlSel,'insIst');
	$aRow = $objDB->fetch('insIst');
	if ($aRow) {
		$msg .= "Codice gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "INSERT INTO ovf_anag_sez SET ";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			$sSql .= $aCX[0]." = '".reqVarAS($aCX[0])."', ";
		}
	}
	$sSql = substr($sSql,0,-2);
	$objDB->exec($sSql,'insRec');
	
	header("Location: ".urlTorna(_t("sezione '%s' inserita",reqVar('cod_sez'))));
	exit();
}
